﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.CustomerService.BLL;
using Koonsoft.CustomerService.Entity;
using System.Web.Security;
using log4net;

namespace CustomerService.ReportIssues
{
    public partial class ReportIssue : PageBase
    {
        private readonly static ILog log = log4net.LogManager.GetLogger(typeof(ReportIssue));

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                Session["File"] = dic;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtIssueTitle.Text))
            {
                AlertMsg("请填写标题");
                return;
            }

            if (string.IsNullOrEmpty(txtCustomersQQ.Text)
                    && string.IsNullOrEmpty(txtCustomersMobile.Text))
            {
                AlertMsg("请填写客户QQ或电话，保证可以正常回访");
                return;
            }
            System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;            
            string filePathBase = "~/Upload/";

            List<IssueAccessorie> accessorie = new List<IssueAccessorie>();
            Guid guid = Guid.NewGuid();

            for (int ifile = 0; ifile < files.Count; ifile++)
            {
                if (files[ifile].FileName.Length > 0)
                {
                    string fileName = Server.UrlDecode(files[ifile].FileName.Split('\\').Last());
                    string filePath = filePathBase + DateTime.Now.ToString("yyyyMMddHHmmss") + fileName;
                    //上传单个文件并保存相关信息
                    System.Web.HttpPostedFile myFile = files[ifile];

                    try
                    {
                        myFile.SaveAs(Page.Server.MapPath(filePath));
                    }
                    catch (Exception ex)
                    {
                        log.Info("==========================错误开始================================");
                        log.Info(filePath);
                        log.Info(ex.Message);
                        log.Info(ex.StackTrace);
                        log.Info("==========================错误结束================================");
                        AlertMsg("文件上传失败，请联系管理员!("+filePath+")");
                        return;
                    }

                    IssueAccessorie acc = new IssueAccessorie();
                    acc.IssueId = guid;
                    acc.Accessory = filePath;
                    acc.AccessoryName = fileName;

                    accessorie.Add(acc);
                }
            }


            Issue issue = new Issue();
            issue.IssueID = guid;
            issue.IssueTitle = txtIssueTitle.Text;
            issue.IssueLevel = dropIssueLevel.SelectedValue;
            issue.IssueCategory = dropIssueCategory.SelectedValue;
            issue.IssueSource = dropIssueSource.SelectedValue;
            issue.ClientVersion = dropClientVersion.SelectedValue;
            issue.CustomersQQ = txtCustomersQQ.Text;
            issue.CustomersMobile = txtCustomersMobile.Text;
            issue.ServiceArea = dropServiceArea.SelectedValue;
            issue.Region = txtRegion.Text.Trim();
            issue.BandWidth = dropBandWidth.SelectedValue;
            issue.SystemEnvironment = dropSystemEnvironment.SelectedValue;
            issue.DecodingRate = txtDecodingRate.Text;
            issue.IsUnblocked = cbIsUnblocked.Checked;
            issue.Mark = txtMark.Text;
            issue.CreateUser = LoginName;
            issue.UserName = txtUserName.Text.Trim();
            issue.UserAccounts = txtUserAccounts.Text.Trim();
            issue.GSID = txtGSID.Text.Trim();
            issue.PrincipalID = dropPrincipal.SelectedValue;
            issue.OperatorsId = dropOperators.SelectedValue;

            string msg = "";
            new IssuesBLL().Create(issue,accessorie, out msg);
            if (string.IsNullOrEmpty(msg))
            {
                AlterAndRedirect("创建成功", "../Default.aspx");
            }
            else
            {
                AlertMsg("创建失败，请联系管理员：" + msg.Split(';'));
            }
        }


        //protected void Button2_Click(object sender, EventArgs e)
        //{
        //    string filePath = "~/Upload/";
        //    // 上传文件
        //    string fileName = Server.UrlEncode(fUpload.FileName);
        //    filePath = filePath + DateTime.Now.ToString("yyyyMMddHHmmss") + fileName;
        //    fUpload.SaveAs(Page.Server.MapPath(filePath));


        //    Dictionary<string, string> dic = Session["File"] as Dictionary<string, string>;
        //    if (dic == null)
        //    {
        //        dic = new Dictionary<string, string>();
        //    }
        //    dic.Add(filePath, fileName);
        //    Label1.Text += "<br />" + fileName;

        //    Session["File"] = dic;
        //}
    }
}